<?php
if (!(defined('IN_IA'))) 
{
    exit('Access Denied');
}

//服务类订单
class Sorder_MxShopModel {

    private $sOrderTable = '';
    private $sOrderCommentTable = '';
    private $pagesize = 10;

    public function __construct()
    {
        global $_W;
        $this->sOrderTable = tablename('mx_shop_service_order');
        $this->sOrderCommentTable = tablename('mx_shop_service_order_comment');

        //获取用户信息
        $this->member = m('userinfo')->getUser($_W['openid']);
    }

    //获取我的订单列表
    public function getMyList()
    {
        global $_W, $_GPC;

        $page = empty($_GPC['page']) ? 1 : intval($_GPC['page']);
        //获取用户信息
        $member = $this->member;

        $pagesize = $this->pagesize;

//        $condition = ' uniacid=:uniacid AND uid=:uid';
        $condition = ' uniacid=:uniacid ';
        $params = array(
            ':uniacid'=>$_W['uniacid'],
//            ':uid'=>$member['id']
        );

        //订单状态
        $status = $_GPC['status'];
        if (!empty($status))
        {
            $condition .= ' AND status=:status';
            $params[':status'] = intval($status);
        }

        //是否回收站
        $type = $_GPC['type'];
        if (!empty($type) && $type == 'isdel')
        {
            $condition .= ' AND isrecycle=1';
        }
        else
        {
            $condition .= ' AND isrecycle=0';
        }

        $condition .= ' AND deleted=0 AND userdeleted=0';

        $sql = 'SELECT * FROM '.$this->sOrderTable;
        $where = ' WHERE '. $condition;
        $orderby = ' ORDER BY id DESC';
        $limit = ' LIMIT '.($page-1) * $pagesize.','.$pagesize;

        $list = pdo_fetchall($sql.$where.$orderby.$limit, $params);

        $sql = 'SELECT ifnull(count(*), 0) FROM '.$this->sOrderTable;
        $total = pdo_fetchcolumn($sql.$where, $params);

        return array('total'=>$total, 'list'=>$list, 'pagesize'=>$pagesize);
    }


    /*
     * 获取当前用户使用中的设备
     * @param string $number 设备号（可选）
     * @return array/null
     */
    public function getInUseSgoodsByNumber($number='')
    {
        global $_W;

        $user = m('userinfo')->getUser($_W['openid']);

        $condition = ' uid=:uid AND status=2 AND ispay=1 AND opentime>0 AND deleted=0 AND userdeleted=0';
        $params = array(':uid' => $user['id']);
        if(!empty($number))
        {
            $sgoods = m('sgoods')->getDetail($number, array('name'));

            $condition .= ' AND goods_id=:goods_id';
            $params[':goods_id'] = $sgoods['id'];
        }

        $order = pdo_fetchall('SELECT * FROM ' . $this->sOrderTable . ' WHERE '.$condition.' ORDER BY opentime ASC ', $params);

        return empty($number) ? $order : $order[0];
    }

    /*
     * 根据订单id获取订单评价
     * @param int $orderid 订单号
     * @return array/null
     */
    public function getCommentDetailByOrderId($orderid=0)
    {
        global $_W;

        if(empty($orderid))
        {
            return null;
        }
        //获取用户信息
        $member = $this->member;

        $sql = 'SELECT * FROM '.$this->sOrderCommentTable;
        $where = ' WHERE uid=:uid AND orderid=:orderid AND uniacid=:uniacid AND deleted=0';
        $limit = ' LIMIT 1';
        $params = array(':uid'=>$member['id'], ':orderid'=>$orderid, ':uniacid'=>$_W['uniacid']);

        return pdo_fetch($sql.$where.$limit, $params);
    }


    /*
     * 根据订单号获取订单详情
     * @param string $ordersn 订单号
     * @return array/null
     */
    public function getDetailByOrdersn($ordersn='')
    {
        global $_W;

        if(empty($ordersn))
        {
            return null;
        }

        //获取用户信息
        $member = $this->member;

        $sql = 'SELECT * FROM '.$this->sOrderTable;
        $where = ' WHERE uid=:uid AND ordersn=:ordersn AND uniacid=:uniacid AND deleted=0 AND userdeleted=0';
        $limit = ' LIMIT 1';
        $params = array(':uid'=>$member['id'], ':ordersn'=>$ordersn, ':uniacid'=>$_W['uniacid']);

        return pdo_fetch($sql.$where.$limit, $params);
    }

    /*
	 * 根据表名获取详情
	 * @param int $t 表名
	 * @param int $id 表id
	 * @param array $select 要查询的字段
	 * @return array/null
 	 * */
    public function getDetailToTableById($t, $id=0, $select=array())
    {
        global $_W;

        if(empty($id))
        {
            return null;
        }

        $info = '*';
        if(!empty($select) && is_array($select))
        {
            $info = array('id');
            foreach ($select as $item)
            {
                if(is_string($item))
                {
                    $info[] = $item;
                }
            }
            $info = implode(',', $info);
        }

        $sql = 'SELECT '.$info.' FROM '.$t;
        $where = ' WHERE id=:id AND uniacid=:uniacid AND status=1 AND deleted=0';
        $limit = ' LIMIT 1';
        $params = array(':id'=>$id, ':uniacid'=>$_W['uniacid']);

        return pdo_fetch($sql.$where.$limit, $params);
    }

}
